﻿using HuaCheng.CommonClass;
using HuaCheng.DataAccess;
using HuaCheng.Model;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace HuaCheng.Views
{
    /// <summary>
    /// 通道复制
    /// </summary>
    public partial class ChlCopy : Form
    {
        public static bool firLine;//第一排是否全选
        public static bool secLine;//第二排是否全选
        public delegate void WeekStepSet(Dictionary<int, string> chls);//委托
        public event WeekStepSet SelectedCopyChl;//事件
        public Dictionary<int, string> selectedCopyChls;//选中复制通道实际id + 通道映射号 如1-1
        //public static bool isRun;//当前通道是否正在运行
        //public static int realId;
        public List<Channel> chlList;
        public ChlCopy(string ip, int gid, int mid, int Id, Dictionary<int, string> chlsCopy, List<Channel> chls)
        {
            InitializeComponent();
            selectedCopyChls = chlsCopy;
            //isRun = isrun;//当前通道是否正在运行
            //realId = Id;
            chlList = chls;//16个通道类集合
            LoadChl(ip, gid, mid);//界面初始化
        }
        /// <summary>
        /// 关闭时触发方法
        /// </summary>
        /// <param name="realIds"></param>
        /// <param name="maps"></param>
        public void OnClose(Dictionary<int, string> chls)
        {
            if (SelectedCopyChl != null)
            {
                SelectedCopyChl(chls);
            }
        }
        /// <summary>
        /// 加载通道排布 并处理撤销的通道
        /// 1x8和1x16两种情况
        /// </summary>
        public void LoadChl(string ip, int gid, int mid)
        {
            List<int> revoIds = ChlMapHelper.QueryRevokeChl(ip, gid, mid);
            if (selectedCopyChls.Count > 0)//再次回来选择时恢复关闭通道复制页面之前选中的复制通道
            {
                foreach (int id in selectedCopyChls.Keys)
                {
                    string chlName = "chBox" + id;
                    CheckBox chbox = (CheckBox)this.pnlContent.Controls[chlName];
                    chbox.Checked = true;
                }
            }
            if (revoIds.Count > 0)//如果有撤销的通道则不允许复制
            {
                foreach (int id in revoIds)
                {
                    string chlName = "chBox" + id;
                    CheckBox chbox = (CheckBox)this.pnlContent.Controls[chlName];
                    chbox.Enabled = false;
                }
            }
            if (chlList.Count > 0)
            {
                for (int i = 0; i < chlList.Count; i++)
                {
                    if (chlList[i].RunStat)//如果通道正在运行不能选择为复制对象
                    {
                        //获取与该通道并联的所有通道号,并联Id是主通道realId,所以这里以i为参数
                        List<int> allParaId = MutiChlHelper.GetChlById(i, ip, gid, mid);
                        if (allParaId.Count == 0)
                        {
                            string chkName = "chBox" + chlList[i].RealId;
                            CheckBox chk = (CheckBox)this.pnlContent.Controls[chkName];
                            chk.Enabled = false;
                        }
                        else//如果该运行通道存在并联通道，则并联通道也不能选择为复制对象
                        {
                            for (int j = 0; j < allParaId.Count; j++)
                            {
                                string chkName = "chBox" + (allParaId[j] - 1);
                                CheckBox chk = (CheckBox)this.pnlContent.Controls[chkName];
                                chk.Enabled = false;
                            }
                        }
                    }
                }
            }

        }
        /// <summary>
        /// 点击check框事件触发并联通道选中事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CheckChanged(object sender, EventArgs e)
        {
            //获取当前选中框
            CheckBox chb = sender as CheckBox;
            int realId = GetRealId(chb);
            //获取与该通道并联的所有通道号
            List<int> allParaId = MutiChlHelper.GetAllChlByOneId(realId, chlList[0].DevIp);
            foreach (int i in allParaId)
            {
                string name = "chBox" + i;
                CheckBox chk = (CheckBox)this.pnlContent.Controls[name];
                chk.Checked = chb.Checked;
            }
        }
        /// <summary>
        /// 根据控件名称获取实际id
        /// </summary>
        /// <param name="chbf"></param>
        /// <returns></returns>
        private static int GetRealId(CheckBox chbf)
        {
            return Convert.ToInt32(chbf.Name.Substring(5));
        }
        /// <summary>
        /// 整柜选中1
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFir_Click(object sender, EventArgs e)
        {
            firLine = !firLine;//取反
            SelectLine(1, firLine);
        }
        /// <summary>
        /// 整柜选中2
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSec_Click(object sender, EventArgs e)
        {
            secLine = !secLine;
            SelectLine(2, secLine);
        }
        /// <summary>
        /// 确定按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            selectedCopyChls.Clear();//先清空原字典中的数据
            for (int i = 0; i < this.pnlContent.Controls.Count; i++)
            {
                string checkName = "chBox" + i;
                CheckBox chb = (CheckBox)this.pnlContent.Controls[checkName];
                if (chb.Checked)
                {
                    int realid = GetRealId(chb);
                    string map = (realid / 8 + 1) + "-" + (realid % 8 + 1);
                    if (!selectedCopyChls.ContainsKey(realid))
                        selectedCopyChls.Add(realid, map);
                }
            }
            //foreach (CheckBox chb in this.pnlContent.Controls)//把选中的通道加到字典中
            //{
            //    if (chb.Checked)
            //    {
            //        int realid = GetRealId(chb);
            //        string map = (realid / 8 + 1) + "-" + (realid % 8 + 1);
            //        if (!selectedCopyChls.ContainsKey(realid))
            //            selectedCopyChls.Add(realid, map);
            //    }
            //}
            if (selectedCopyChls.Count == 0)
            {
                if (IndexPage.language == "en-US")
                {
                    MessageBox.Show("Empty", "Tip", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    MessageBox.Show("没有选取任何通道！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                OnClose(selectedCopyChls);//把复制通道号传回复制工步界面
            }
            this.Close();
        }
        /// <summary>
        /// 取消按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 为指定行全选或者取消全选
        /// </summary>
        /// <param name="line"></param>
        /// <param name="check"></param>
        public void SelectLine(int line, bool check)
        {
            if (line == 1)//第一行全选/取消全选
            {
                for (int i = 0; i < 8; i++)
                {
                    string checkName = "chBox" + i;
                    CheckBox chk = (CheckBox)this.pnlContent.Controls[checkName];
                    if (chk.Enabled)
                    {
                        chk.Checked = check;
                    }
                }
            }
            else//第二行全选/取消全选
            {
                for (int i = 8; i < 16; i++)
                {
                    string checkName = "chBox" + i;
                    CheckBox chk = (CheckBox)this.pnlContent.Controls[checkName];
                    if (chk.Enabled)
                    {
                        chk.Checked = check;
                    }
                }
            }
        }

        #region 国际化
        private void ChlCopy_Load(object sender, EventArgs e)
        {
            ResourceCulture.SetCurrentCulture(IndexPage.language);
            this.SetResourceCulture();
        }
        /// <summary>
        /// 为当前页面内所有控件设置文本语种
        /// </summary>
        private void SetResourceCulture()
        {
            this.Text = ResourceCulture.GetString("CopyChannel");
            this.btnCancel.Text = ResourceCulture.GetString("LoginCancel");
            this.btnOk.Text = ResourceCulture.GetString("LoginOk");
        }
        #endregion
    }
}
